package com.bot.rf.sql.util;

import java.sql.*;

import static com.bot.rf.config.UserConfig.*;
import static com.bot.rf.util.MyLogger.LOGGER;
import static com.bot.rf.util.MyLogger.getTrace;

/**
 * @program: report-form-demo
 * @description: 操作用户MySQL工具类
 * @author: zyy
 * @create: 2021-03-01 11:31
 **/
public class MySqlUtil {
    /**
     * 数据库连接、查询、结果集
     */
    private static Connection conn = null;
    private static Statement stmt = null;
    private static PreparedStatement pstmt = null;
    private static ResultSet resultSet = null;

    //--------------------------- 用户MySQL配置 start -----------------------------
    /**
     * url格式  jdbc:mysql://localhost:3306/tooker_service
     */
    private static String URL_OTHER_PARAMS = "serverTimezone=Asia/Shanghai&allowMultiQueries=true&useUnicode=true&" +
            "characterEncoding=UTF-8&useSSL=false";
    private static String URL = "jdbc:mysql://" + USER_DATABASE_HOST + ":" + USER_DATABASE_PORT + "/" +
            USER_DATABASE_NAME + "?" + URL_OTHER_PARAMS;
    /**
     * 表名
     */
    private static String TABLE_NAME = USER_MAIN_TABLE_NAME;

    /**
     * 账号和密码
     */
    private static String USER_NAME = USER_DATABASE_USERNAME;
    private static String PASSWORD = USER_DATABASE_PASSWORD;
    //--------------------------- 用户MySQL配置  end  -----------------------------

    //--------------------------- 配置方法 start -------------------------------
    /**
     * 创建连接
     *
     * @return
     */
    public static boolean createOrConnect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            LOGGER.info("连接成功...");
        } catch (Exception e) {
            LOGGER.error(getTrace(e));
            LOGGER.info("URL：" + URL);
            return false;
        }
        return true;
    }

    /**
     * 关闭自动提交
     * @return
     */
    public static boolean closeAutoCommit() {
        try {
            if (conn == null || conn.isClosed()) {
                createOrConnect();
            }
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 提交
     * @return
     */
    public static boolean commit() {
        try {
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 创建 Statement
     * @return
     */
    public static boolean createStatement() {
        try {
            if (stmt != null && !stmt.isClosed()) {
                return true;
            }
            System.out.println("Sql Statement关闭，重新连接...");
            stmt = conn.createStatement();
        } catch (SQLException e) {
            LOGGER.error(getTrace(e));
            return false;
        }
        return true;
    }

    /**
     * 关闭连接
     */
    public static void closeConnect() {
        try {
            if (stmt != null && !stmt.isClosed()) {
                stmt.close();
            }
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            LOGGER.error(e.getMessage());
        }
    }
    //--------------------------- 配置方法  end  -------------------------------

    // 自定义查询方法，参考oracle的例子
}
